package com.jy.api.service.impl;

import com.jy.api.controller.admin.params.InventoryFlowRequest;
import com.jy.api.dao.*;
import com.jy.api.domain.*;
import com.jy.api.exception.MyException;
import com.jy.api.payRes.InventoryDto;
import com.jy.api.service.InventoryService;
import com.jy.api.service.StockReceiptSerivce;
import com.jy.api.service.StockTransactionService;
import com.jy.api.signUpReq.InvStatisticsRes;
import com.jy.api.signUpReq.stock.*;
import com.jy.api.util.MyDateUtils;
import com.jy.api.util.RedisSyncLocker;
import com.jy.api.util.Result;
import com.jy.api.vo.stock.InventoryDataVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * Created by hanxi on 2018/7/5.
 */

@Slf4j
@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    InventoryDao inventoryDao;
    @Autowired
    StockTransactionService stockTransactionService;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private StoreDao storeDao;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private StockCheckOrderDao stockCheckOrderDao;
    @Autowired
    private StockTransactionDao stockTransactionDao;
    @Autowired
    private StockReceiptSerivce stockReceiptSerivce;
    @Autowired
    private StockReceiptDao stockReceiptDao;
    @Autowired
    private RedisSyncLocker redisSyncLocker;
    @Autowired
    private ProductMaterialDao productMaterialDao;
    @Autowired
    private MaterialUnitDao materialUnitDao;


    /**
     * @param storeId
     * @param page
     * @return
     */

    @Override
    public Page<Inventory> findByStoreId(String storeId, ProductMold mold, Pageable page) {
        Page<Inventory> byStoreIdAndNameLike = inventoryDao.findByStoreIdAndEnabledAndMoldOrderByProductTypeDesc(storeId, true, mold, page);
        return byStoreIdAndNameLike;
    }

    /**
     * 查询店铺
     *
     * @param storeId
     * @param page
     * @return
     */
    @Override
    public Page<Product> findByStoreIdAndIsStockEnable(String storeId, Pageable page) {
        Page<Product> list = productDao.findListByStockEnabledAndEnabledAndStoreId(true, true, storeId, page);
        return list;
    }

    /**
     * 修改进货价格
     *
     * @param inventoryDataVo
     */
    @Override
    @Transactional
    public void updateByStoreId(InventoryDataVo inventoryDataVo) {
        Inventory one = inventoryDao.findById(inventoryDataVo.getId());
        if (one != null) {
            one.setPurchasePrice(inventoryDataVo.getPrice());
            inventoryDao.save(one);
        }


    }

    /**
     * 通过id查库存ID
     *
     * @param id
     * @param pageable
     * @return
     */
    @Override
    public Page<Inventory> findByProductId(String id, Pageable pageable) {
        Page<Inventory> page = inventoryDao.findByEnabledAndProductId(true, id, pageable);
        return page;
    }


    @Override
    public List<Inventory> findByPId(String id) {
        return inventoryDao.findByEnabledAndProductId(true, id);
    }

    /**
     * 通过商品ID查询吧台商品
     *
     * @param id
     * @return
     */
    @Override
    public Page<Product> findCountByProduct(String id, Pageable pageable) {
        Page<Product> list = productDao.findListByStockEnabledAndEnabledAndId(true, true, id, pageable);
        return list;
    }


    /**
     * 仓库入库
     *
     * @param req
     * @return
     */
    @Override
    @Transactional
    public Result stockIn(InventoryInReq req) throws Exception {
        Store store = storeDao.findOne(req.getStoreId());
        if (store == null) {
            return new Result(400, "无该店铺信息");
        }
        Employee employee = employeeDao.findOne(req.getOperatorId());
        if (employee == null) {
            return new Result(400, "无该操作人的信息");
        }
        List<InventoryFlowRequest> requests = new ArrayList<>();
        if (req.getStockSource().equals(StockChangeReason.INNER_IN)) {
            for (InventoryInReq.Inventore in : req.getInventores()) {
                //仓库入库参数构建
                Inventory inventory = inventoryDao.findById(in.getInvId());
                InventoryFlowRequest stockIn = new InventoryFlowRequest();
                stockIn.setSkuId(in.getInvId() + "");
                stockIn.setSkuName(in.getProductName() + "[" + in.getCapacity() + "/件" + "]");
                stockIn.setStockLevel(StockLevel.WAREHOUSE);
                stockIn.setStoreId(req.getStoreId());
                stockIn.setType(StockType.STOCK_IN);
                stockIn.setSource(StockChangeReason.BAR_IN);
                stockIn.setOperatorId(req.getOperatorId());
                stockIn.setOperatorName(req.getOperatorName());
                stockIn.setNotes(req.getNotes());
                stockIn.setMold(req.getMold());
                stockIn.setQty(in.getQuantity());
                if (req.getMold().equals(ProductMold.MATERIAL)) {
                    ProductMaterial material = productMaterialDao.findOne(Integer.valueOf(inventory.getProductId()));
                    if (material != null) {
                        MaterialUnit unit = materialUnitDao.findOne(material.getUnitId());
                        stockIn.setUnitName(unit.getName());
                    }
                }
                //吧台出库参数构建
                InventoryFlowRequest stockOut = new InventoryFlowRequest();
                stockOut.setSkuId(inventory.getProductId() + "");
                stockOut.setSkuName(in.getProductName());
                if (req.getMold().equals(ProductMold.MATERIAL)) {
                    stockOut.setSkuType(2);
                }
                if (req.getMold().equals(ProductMold.PRODUCT)) {
                    stockOut.setSkuType(1);
                }
                stockOut.setStockLevel(StockLevel.BAR);
                stockOut.setStoreId(req.getStoreId());
                stockOut.setType(StockType.STOCK_OUT);
                stockOut.setSource(StockChangeReason.BAR_OUT);
                stockOut.setOperatorId(req.getOperatorId());
                stockOut.setOperatorName(req.getOperatorName());
                stockOut.setNotes(req.getNotes());
                stockOut.setMold(req.getMold());
                stockOut.setQty(-in.getQuantity() * inventory.getCapacity());
                if (req.getMold().equals(ProductMold.MATERIAL)) {
                    ProductMaterial material = productMaterialDao.findOne(Integer.valueOf(inventory.getProductId()));
                    if (material != null) {
                        MaterialUnit unit = materialUnitDao.findOne(material.getUnitId());
                        stockOut.setUnitName(unit.getName());
                    }
                }
                requests.add(stockOut);
                requests.add(stockIn);
            }
        } else {
            for (InventoryInReq.Inventore in : req.getInventores()) {
                Inventory inventory = inventoryDao.findById(in.getInvId());
                InventoryFlowRequest stockIn = new InventoryFlowRequest();
                stockIn.setSkuId(in.getInvId() + "");
                stockIn.setSkuName(in.getProductName() + "[" + in.getCapacity() + "/件" + "]");
                stockIn.setStockLevel(StockLevel.WAREHOUSE);
                stockIn.setStoreId(req.getStoreId());
                stockIn.setType(StockType.STOCK_IN);
                stockIn.setSource(req.getStockSource());
                stockIn.setOperatorId(req.getOperatorId());
                stockIn.setOperatorName(req.getOperatorName());
                stockIn.setNotes(req.getNotes());
                stockIn.setMold(req.getMold());
                stockIn.setQty(in.getQuantity());
                if (req.getMold().equals(ProductMold.MATERIAL)) {
                    ProductMaterial material = productMaterialDao.findOne(Integer.valueOf(inventory.getProductId()));
                    if (material != null) {
                        MaterialUnit unit = materialUnitDao.findOne(material.getUnitId());
                        stockIn.setUnitName(unit.getName());
                    }
                }
                requests.add(stockIn);
            }
        }
        log.info("保存之后的transaction");
        stockTransactionService.flows(requests);
        return new Result(200, "入库成功");
    }

    /**
     * 仓库出库
     *
     * @param inReq
     * @return
     */
    @Override
    @Transactional
    public Result stockOut(InventoryInReq inReq) throws Exception {
        StockReceipt stockReceipt = stockReceiptSerivce.out(inReq);
        List<InventoryInReq.Inventore> list = inReq.getInventores();
        /**
         * 对仓库库存减少，对吧台库存增加
         */
        //库存批量变动参数[{"mold":"MATERIAL","notes":"","operatorId":"a12606e3f0ee4e6096abc7cd2f19be5c",
        // "operatorName":"店主","qty":10,"skuId":"34","skuName":"700ML","skuType":0,"source":"STOCK_INNER_IN",
        // "stockLevel":"BAR","storeId":"574b59ef-b19c-11e7-9233-37891743d141","type":"STOCK_IN","unitName":"个"},
        // {"mold":"MATERIAL","notes":"","operatorId":"a12606e3f0ee4e6096abc7cd2f19be5c","operatorName":"店主","qty":-10,
        // "skuId":"3309","skuName":"700ML[1/件]","skuType":2,"source":"INNER_OUT","stockLevel":"WAREHOUSE","storeId":
        // "574b59ef-b19c-11e7-9233-37891743d141","type":"STOCK_OUT","unitName":"个"}]
        List<InventoryFlowRequest> requests = new ArrayList<>();
        if (inReq.getStockSource().equals(StockChangeReason.INNER_OUT)) {
            for (InventoryInReq.Inventore in : list) {
                Inventory inventory = inventoryDao.findById(in.getInvId());
                InventoryFlowRequest stockIn = new InventoryFlowRequest();
                stockIn.setSkuId(in.getInvId() + "");
                stockIn.setSkuName(in.getProductName() + "[" + in.getCapacity() + "/件" + "]");
                stockIn.setStockLevel(StockLevel.WAREHOUSE);
                stockIn.setStoreId(inReq.getStoreId());
                stockIn.setType(StockType.STOCK_OUT);
                stockIn.setSource(StockChangeReason.INNER_OUT);
                stockIn.setOperatorId(inReq.getOperatorId());
                stockIn.setOperatorName(inReq.getOperatorName());
                stockIn.setNotes(inReq.getNotes());
                stockIn.setMold(inReq.getMold());
                stockIn.setQty(-in.getQuantity());
                if (inReq.getMold().equals(ProductMold.MATERIAL)) {
                    ProductMaterial material = productMaterialDao.findOne(Integer.valueOf(inventory.getProductId()));
                    if (material != null) {
                        MaterialUnit unit = materialUnitDao.findOne(material.getUnitId());
                        stockIn.setUnitName(unit.getName());
                    }
                }
                //吧台出库参数构建
                InventoryFlowRequest stockOut = new InventoryFlowRequest();
                stockOut.setSkuId(inventory.getProductId() + "");
                stockOut.setSkuName(in.getProductName());
                if (inReq.getMold().equals(ProductMold.MATERIAL)) {
                    stockOut.setSkuType(2);
                }
                if (inReq.getMold().equals(ProductMold.PRODUCT)) {
                    stockOut.setSkuType(1);
                }
                stockOut.setStockLevel(StockLevel.BAR);
                stockOut.setStoreId(inReq.getStoreId());
                stockOut.setType(StockType.STOCK_IN);
                stockOut.setSource(StockChangeReason.STOCK_INNER_IN);
                stockOut.setOperatorId(inReq.getOperatorId());
                stockOut.setOperatorName(inReq.getOperatorName());
                stockOut.setNotes(inReq.getNotes());
                stockOut.setMold(inReq.getMold());
                stockOut.setQty(in.getQuantity() * in.getCapacity());
                if (inReq.getMold().equals(ProductMold.MATERIAL)) {
                    ProductMaterial material = productMaterialDao.findOne(Integer.valueOf(inventory.getProductId()));
                    if (material != null) {
                        MaterialUnit unit = materialUnitDao.findOne(material.getUnitId());
                        stockOut.setUnitName(unit.getName());
                    }
                }
                requests.add(stockOut);
                requests.add(stockIn);

            }
        } else {
            for (InventoryInReq.Inventore in : list) {
                Inventory inventory = inventoryDao.findById(in.getInvId());
                InventoryFlowRequest stockIn = new InventoryFlowRequest();
                stockIn.setSkuId(in.getInvId() + "");
                stockIn.setSkuName(in.getProductName() + "[" + in.getCapacity() + "/件" + "]");
                stockIn.setStockLevel(StockLevel.WAREHOUSE);
                stockIn.setStoreId(inReq.getStoreId());
                stockIn.setType(StockType.STOCK_OUT);
                stockIn.setSource(inReq.getStockSource());
                stockIn.setOperatorId(inReq.getOperatorId());
                stockIn.setOperatorName(inReq.getOperatorName());
                stockIn.setNotes(inReq.getNotes());
                stockIn.setMold(inReq.getMold());
                stockIn.setQty(-in.getQuantity());
                if (inReq.getMold().equals(ProductMold.MATERIAL)) {
                    ProductMaterial material = productMaterialDao.findOne(Integer.valueOf(inventory.getProductId()));
                    if (material != null) {
                        MaterialUnit unit = materialUnitDao.findOne(material.getUnitId());
                        stockIn.setUnitName(unit.getName());
                    }
                }
                requests.add(stockIn);
            }
        }
        stockTransactionService.flows(requests);
        return new Result(200, "出库成功");
    }


    @Override
    @Transactional
    public void addNewCapacity(InventoryReqData inventoryReqData) {
        Inventory in = inventoryDao.findByProductIdAndCapacity(inventoryReqData.getProductId(), inventoryReqData.getCapacity());
        String name = "";
        if (inventoryReqData.getMold().equals(ProductMold.PRODUCT)) {
            Product product = productDao.findOne(in.getProductId());
            name = product.getName();
        } else {
            ProductMaterial material = productMaterialDao.findOne(Integer.valueOf(inventoryReqData.getProductId()));
            name = material.getName();
        }
        if (in == null) {
            in = new Inventory(inventoryReqData, 0, 0.0);
        } else {
            if (!in.isEnabled()) {
                in.setName(name);
                in.setEnabled(true);
                in.setPurchasePrice(0.0);
                in.setQuantity(0);
            } else {
                throw new MyException("该规格已存在");
            }
        }
        if (inventoryReqData.getMold().equals(ProductMold.MATERIAL)) {
            MaterialUnit unit = materialUnitDao.findOne(inventoryReqData.getUnitId());
            if (unit != null) {
                in.setUnitId(unit.getId());
                in.setUnitName(unit.getName());
            }
        }
        inventoryDao.save(in);
    }

    /**
     * 通过商品Id查询规格
     *
     * @param productId
     * @return
     */
    @Override
    public List<Map<String, Object>> findByInvProductId(String productId, String storeId) {
        List<Object> list = inventoryDao.findInvByProductId(productId, true, storeId);
        List<Map<String, Object>> ob = new ArrayList<>();
        for (Object o : list) {
            Object[] objects1 = (Object[]) o;
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", objects1[0]);
            map.put("capacity", objects1[1]);
            ob.add(map);
        }
        return ob;
    }

    /**
     * 通过id删除inv
     *
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public Result deleteByInvId(long id) {
        Inventory inventory = inventoryDao.findById(id);
        if (inventory != null) {
            if (inventory.getQuantity() == 0) {
                inventory.setEnabled(false);
                inventoryDao.save(inventory);
                return new Result(200, "删除成功");
            } else {
                return new Result(400, "删除失败，该规格下还有商品");
            }

        }
        return new Result(400, "该规格不存在");
    }

    /**
     * 通过店铺id查询库存列表
     *
     * @param storeId
     * @param
     * @return
     */

    /**
     * 通过店铺id查询库存列表
     *
     * @param storeId
     * @param
     * @return
     */
    @Override
    public List<InventoryDto> findListByStoreId(String storeId, ProductMold mold) {
        List<Inventory> listByEnabledAndStoreId = inventoryDao.findListByEnabledAndStoreIdAndMold(true, storeId, mold);
        List<InventoryDto> inventoryDtos = new ArrayList<>();
        for (Inventory inventory : listByEnabledAndStoreId) {
            InventoryDto dto = new InventoryDto();
            BeanUtils.copyProperties(inventory, dto);
            Product product = productDao.findOne(inventory.getProductId());
            if (product != null) {
                dto.setTypeName(product.getParentTypeName());
            } else {
                dto.setTypeName("");
            }
            inventoryDtos.add(dto);
        }
        return inventoryDtos.stream().sorted(Comparator.comparing(InventoryDto::getTypeName).reversed()).collect(Collectors.toList());

    }

    /**
     * 盘点记录
     *
     * @param req
     * @return
     */
    @Override
    @Transactional
    public Result checkInOrOut(InventoryCheckReq req) throws Exception {
        //保存盘点主记录
        StockCheck stockCheck = new StockCheck(req, StockLevel.WAREHOUSE);
        stockCheck.setMold(req.getMold());
        List<StockCheckTransaction> list = new ArrayList<>();
        //盘点更改库存
        if (null == storeDao.findOne(req.getStoreId())) {
            return new Result(400, "无店铺信息");
        }
        if (null == employeeDao.findOne(req.getOperatorId())) {
            return new Result(400, "无改店员信息");
        }


        List<InventoryFlowRequest> requests = new ArrayList<>();
        for (InventoryCheckReq.CheckInventory in : req.getCheckInventories()) {
            String s = in.checkSockType();
            if (s.equals("NO")) {
                /**
                 * 数量相等，只执行生成盘点记录
                 */
                //生成盘点记录
                StockCheckTransaction s1 = new StockCheckTransaction(in, req.getStoreId(), req.getOperatorId(), req.getOperatorName());
                s1.setMold(req.getMold());
                list.add(s1);
            } else if (s.equals("CHECK-OUT")) {
                /**
                 * 数量不等，执行更改库存操作，并且生成出库记录
                 */
                StockCheckTransaction s1 = new StockCheckTransaction(in, req.getStoreId(), req.getOperatorId(), req.getOperatorName());
                s1.setMold(req.getMold());
                list.add(s1);

                //吧台出库参数构建
                InventoryFlowRequest stockOut = new InventoryFlowRequest();
                stockOut.setSkuId(in.getInvId() + "");
                stockOut.setSkuName(in.getProductName() + "[" + in.getCapacity() + "/件" + "]");
                stockOut.setSkuType(0);
                stockOut.setStockLevel(StockLevel.WAREHOUSE);
                stockOut.setStoreId(req.getStoreId());
                stockOut.setType(StockType.STOCK_OUT);
                stockOut.setSource(StockChangeReason.CHECK_OUT);
                stockOut.setOperatorId(req.getOperatorId());
                stockOut.setOperatorName(req.getOperatorName());
                stockOut.setNotes(req.getNotes());
                stockOut.setMold(req.getMold());
                stockOut.setQty(in.getCheckWholeQty());
                requests.add(stockOut);
            } else {
                /**
                 * 数量不等，执行更改库存操作，并且生成入库记录
                 */
                StockCheckTransaction s1 = new StockCheckTransaction(in, req.getStoreId(), req.getOperatorId(), req.getOperatorName());
                s1.setMold(req.getMold());
                list.add(s1);
                InventoryFlowRequest stockOut = new InventoryFlowRequest();
                stockOut.setSkuId(in.getInvId() + "");
                stockOut.setSkuName(in.getProductName() + "[" + in.getCapacity() + "/件" + "]");
                stockOut.setSkuType(0);
                stockOut.setStockLevel(StockLevel.WAREHOUSE);
                stockOut.setStoreId(req.getStoreId());
                stockOut.setType(StockType.STOCK_IN);
                stockOut.setSource(StockChangeReason.CHECK_IN);
                stockOut.setOperatorId(req.getOperatorId());
                stockOut.setOperatorName(req.getOperatorName());
                stockOut.setNotes(req.getNotes());
                stockOut.setMold(req.getMold());
                stockOut.setQty(in.getCheckWholeQty());
                requests.add(stockOut);
            }
        }
        stockTransactionService.flows(requests);
        stockCheck.setItems(list);
        stockCheckOrderDao.save(stockCheck);
        return new Result(200, "盘点成功");
    }

    /**
     * 库存记录组合查询
     *
     * @param req
     * @param pageable
     * @return
     */

    @Override
    public Page<StockChangeTransaction> findByProductIdAndCapacity(InventorySerachReq req, Pageable pageable) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Specification<StockChangeTransaction> spec = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (!req.getReason().equals(StockChangeReason.ALL)) {
                predicates.add(criteriaBuilder.equal(root.get("source"), req.getReason()));
            }
            if (!req.getType().equals(StockType.ALL)) {
                predicates.add(criteriaBuilder.equal(root.get("type"), req.getType()));
            }
            if (!StringUtils.isBlank(req.getInvId())) {
                predicates.add(criteriaBuilder.equal(root.get("skuId"), req.getInvId()));
            }


            if (!StringUtils.isBlank(req.getBegin()) && !StringUtils.isBlank(req.getEnd())) {
                try {
                    predicates.add(criteriaBuilder.between(root.get("addTime"), sdf.parse(req.getBegin()), sdf.parse(req.getEnd())));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            predicates.add(criteriaBuilder.equal(root.get("mold"), req.getMold()));
            predicates.add(criteriaBuilder.equal(root.get("stockLevel"), StockLevel.WAREHOUSE));
            predicates.add(criteriaBuilder.equal(root.get("storeId"), req.getStoreId()));
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Page<StockChangeTransaction> p = stockTransactionDao.findAll(spec, new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), new Sort(Sort.Direction.DESC, "addTime")));
        return p;
    }

    @Override
    public List<Product> findByProduct(String productId) {
        return productDao.findListByStockEnabledAndEnabledAndId(true, true, productId);
    }

    @Override
    @Transactional
    public Map<String, Object> saveCa(CapReq req) {
        Map<String, Object> map = new HashMap<>();
        Inventory inventory = inventoryDao.findByProductIdAndCapacity(req.getProductId(), req.getCapacity());
        if (null == inventory) {
            inventory = new Inventory(req);
            map.put("data", inventory);
            map.put("msg", "操作成功");
        } else {
            if (!inventory.isEnabled()) {
                inventory.setEnabled(true);
                inventory.setPurchasePrice(req.getPurchasePrice());
                inventory.setQuantity(0);
                map.put("data", inventory);
                map.put("msg", "操作成功");
            } else {
                List list = new ArrayList();
                map.put("data", list);
                map.put("msg", "该规格已存在，请核实！");
            }
        }
        if (req.getMold().equals(ProductMold.MATERIAL)) {
            MaterialUnit unit = materialUnitDao.findOne(req.getUnitId());
            if (unit != null) {
                inventory.setUnitId(unit.getId());
                inventory.setUnitName(unit.getName());
            }
        }
        inventory.setMold(req.getMold());
        inventoryDao.save(inventory);
        return map;
    }

    @Override
    public Page<StockChangeTransaction> findStatisticsList(String storeId, int year, int month, Pageable pageable) {
        String date = year + "-" + month;
        Date start = MyDateUtils.getMinMonthDate(date);
        Date end = MyDateUtils.getMaxMonthDate(date);
        Page<StockChangeTransaction> stockChangeTransactions = stockTransactionDao.findByStoreIdAndAddTimeBetweenAndTypeAndSource(storeId, start, end, StockType.STOCK_IN, StockChangeReason.PURCHASE_IN, pageable);
        return stockChangeTransactions;
    }

    @Override
    public InvStatisticsRes getStatistics(String storeId, String year, String month) {
        String date = year + "-" + month;
        InvStatisticsRes res = new InvStatisticsRes();
        List<Object[]> objects;
        objects = stockReceiptDao.findStatistics(storeId, "STOCK_IN", "PURCHASE_IN", date);
        for (Object[] obj : objects) {
            if (null == obj[0] || null == obj[1]) {
                res.setAmount("0.0");
            } else {
                res.setAmount(obj[0].toString());
                res.setCount(obj[1].toString());
            }
        }
        return res;
    }


}
